package itd.newbee.mall.newbeemall.service.impl;

import itd.newbee.mall.newbeemall.Dao.MallUserMapper;
import itd.newbee.mall.newbeemall.Dao.NewBeeMallUserTokenMapper;
import itd.newbee.mall.newbeemall.common.ServiceResultEnum;
import itd.newbee.mall.newbeemall.entity.MallUser;
import itd.newbee.mall.newbeemall.entity.MallUserToken;
import itd.newbee.mall.newbeemall.service.NewBeeMallUserService;
import itd.newbee.mall.newbeemall.util.NumberUtil;
import itd.newbee.mall.newbeemall.util.SystemUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2025/3/10 20:35
 */
@Service
public class NewBeeMallUserServiceImpl implements NewBeeMallUserService {
    @Autowired
    private NewBeeMallUserTokenMapper newBeeMallUserTokenMapper;
    @Autowired
    private MallUserMapper mallUserMapper;

    /**
     * 用户登录业务
     * @param loginName
     * @param passwordMD5
     * @return
     */
    @Override
    public String login(String loginName, String passwordMD5) {
        MallUser user = mallUserMapper.selectByLoginNameAndPasswd(loginName, passwordMD5);
        //找到用户
        if (user != null) {
            //  用户锁定，无法登陆
            if (user.getLockedFlag().intValue() == 1) {
                return ServiceResultEnum.LOGIN_USER_LOCKED_ERROR.getResult();
            }
//            用户没有被锁定
                //登录后执行修改token的操作
                    // 生成token
            String token = getNewToken(System.currentTimeMillis()+"",user.getUserId());
                    //查看用户是否有token
            MallUserToken mallUserToken = newBeeMallUserTokenMapper.selectByPrimaryKey(user.getUserId());
            //当前时间
            Date now = new Date();
            //过期时间两天
            Date expireTime = new Date(now.getTime() + 2 * 24 * 3600 * 1000);
                    // 如果mallUserToken为空，则生成一个mallUserToken对象
            if (mallUserToken == null) {
                mallUserToken = new MallUserToken();
                mallUserToken.setUserId(user.getUserId());
                mallUserToken.setToken(token);
                mallUserToken.setUpdateTime(now);
                mallUserToken.setExpireTime(expireTime);
                if(newBeeMallUserTokenMapper.insertSelective(mallUserToken)>0){
                    //添加成功
                    return token;
                }
            }else {// mallUserToken不为空，则更新mallUserToken
                    mallUserToken.setToken(token);
                    mallUserToken.setUpdateTime(now);
                    mallUserToken.setExpireTime(expireTime);
                    if(newBeeMallUserTokenMapper.updateByPrimaryKeySelective(mallUserToken)>0){
                        //更新成功
                        return token;
                    }
                }

            }//没有找到用户，返回登录失败
            return ServiceResultEnum.LOGIN_ERROR.getResult();

        }



    /**
     * 获取token值
     * @param timeStr
     * @param userId
     * @return
     */
    private String getNewToken(String timeStr,Long userId) {
        String src = timeStr + userId + NumberUtil.genRandomNum(4);


        return  SystemUtil.getToken(src);
    }
}
